/*
 * ReplyScreen.java
 *
 * ?<your company here>, 2003-2005
 * Confidential and proprietary.
 */

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.*;
import javax.microedition.rms.*;

public class ReplyScreen extends MainScreen {
            private OpenPop _app;
            private AutoTextEditField _content;
            private EditField _sendto;
            private String _toAddress;
            private String _toName;
            private int _toPos;
            private int _toIndex;
            public void setTo (String toName,String toAddress,int toPos,int toIndex) {
                    _toName=toName;
                    _toAddress=toAddress;
                    _toPos=toPos;
                    _toIndex=toIndex;
            }        
            public ReplyScreen (OpenPop uiapp) {
                 super();
                 _app=uiapp;                
                 setTitle(new LabelField("Reply", LabelField.ELLIPSIS
                                    | LabelField.USE_ALL_WIDTH));
                 _content=new AutoTextEditField("Message:\n","");
                 _sendto=new EditField("Reply to: ","",20,EditField.NON_FOCUSABLE);
                 add(_sendto);
                 add(_content);
            }            
            private MenuItem _exitMenuItem = new MenuItem("Exit", 110, 10) {
                public void run()
                {
                    close();                
                }
            };
            private MenuItem _sendMenuItem = new MenuItem("Send", 110, 10) {
                public void run()
                {
                    String content=_content.getText().trim();                   
                    if (content.length()==0) {
                        return;
                    }
                    _app.send(_toAddress,content);
                    _app.updateReply(_toPos,_toIndex,content);
                    close();
                    
                }
            };
            protected void makeMenu(Menu menu, int instance) {                   
                    menu.add(_sendMenuItem);
                    menu.add(_exitMenuItem);                       
            }             
            public boolean onSavePrompt() {
                    return true;
            }                           
            public boolean onClose() {
                    
                    if (_content.getText().length()!=0) {
                        _content.setText("");
                        return false;
                    }
                    System.out.println(_content.getText().length());
                    return super.onClose();                  
            }
            protected boolean keyChar(char key, int status, int time) {
                    if (key==Characters.DELETE) {
                             _sendMenuItem.run();
                             return true;
                    }
                    return super.keyChar(key, status, time);
            }
            public void display() {
                    _sendto.setText(_toName);
                    _app.pushScreen(this);
            }
}
